package demoMod.invincibleOne.cards.invincible;

import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.watcher.ChooseOneAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.InvinciblePower;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.cards.optionCards.ChooseMonsterCard;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.powers.InvinciblePower2;
import demoMod.invincibleOne.powers.TauntPower;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HoldGround extends AbstractInvincibleCard {
    public static final String ID = InvincibleOneMod.makeID("HoldGround");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/HoldGround.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.RARE;
    private static final CardTarget TARGET = CardTarget.ALL_ENEMY;

    private static final int COST = -2;

    public HoldGround() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 70;
        this.tags.add(CardTagsEnum.VIRUS);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        List<AbstractMonster> monsters = AbstractDungeon.getMonsters().monsters.stream().filter(mo -> !mo.isDeadOrEscaped()).collect(Collectors.toList());
        if (!monsters.isEmpty()) {
            if (!upgraded) {
                AbstractMonster target = monsters.get(AbstractDungeon.cardRandomRng.random(monsters.size() - 1));
                addToBot(new ApplyPowerAction(target, target, new InvinciblePower2(target, this.magicNumber)));
            } else {
                if (monsters.size() >= 2) {
                    ArrayList<AbstractCard> options = new ArrayList<>();
                    for (AbstractMonster monster : monsters) {
                        AbstractCard option = new ChooseMonsterCard(monster, () -> {
                            addToTop(new ApplyPowerAction(monster, monster, new InvinciblePower2(monster, this.magicNumber)));
                            if (upgraded) {
                                addToTop(new ApplyPowerAction(monster, monster, new TauntPower(monster)));
                            }
                        });
                        options.add(option);
                    }
                    addToBot(new ChooseOneAction(options));
                } else {
                    AbstractMonster target = monsters.get(0);
                    addToTop(new ApplyPowerAction(target, target, new InvinciblePower2(target, this.magicNumber)));
                    if (upgraded) {
                        addToTop(new ApplyPowerAction(target, target, new TauntPower(target)));
                    }
                }
            }
        }
    }
}
